Load Libraries
#Load Libraries#
library(tidyverse)
library(plotly)
Load Data
# Load Data
data(msleep)
view(msleep)
Assign & Clean Data to Plot
msleep <- ggplot2::msleep %>% # we need to assign the data set "msleep" found in ggplot and assign it to our current RMarkdown.
rename(`Type of Diet` = vore, `Total Hours of Sleep` = sleep_total) %>% #changes names before making graph so axis doesn't need to be changed later
na.omit() #removes any incomplete or N/A data from data set
Creating a Bar Graph
# Create a bar graph
Q <- msleep %>% # We have assigned our graph the letter Q to request it easily later
plot_ly(x = ~reorder(`Type of Diet`, -`Total Hours of Sleep`),
y = ~`Total Hours of Sleep`, # changing the order of how the bars show up so they're increasing in hours of sleep.
type = 'bar', # type of graph
marker = list(color = rainbow(nrow(msleep), #uses rainbow colors for bars indicating their sleeping hours individually and as a dietary group.
alpha = 0.7))) %>% #determines the transparency/opacity of the bars 0.0 being completely transparent
layout(xaxis = list(title = "Type of Diet"), #naming x-axis
yaxis = list(title = "Total Hours of Sleep"), #naming y-axis
title = "Total Hours of Sleep vs. Type of Diet") #naming graph
Animating your Graph
# Add animation to the bars
Q <- Q %>%
animation_opts(frame = 100, easing = 'elastic') #controls timing and style of animation,
Displaying our Results
# Display the graph
Q